<template>
  <div class="air">
    <el-row :gutter="24">
        <el-col :span="24" class="paddCol" >
            <el-card class="box-card">
              <div slot="header" class="clearfix">
                  <span :style="this.alarmstate == 0 ? '':'color:#ea2e2e'"><i class="el-icon-menu"></i> <span class="shebeiname">{{this.data.name}}</span></span>
              </div>
              <div class="temp_card">
                  <!-- <div class="mouse_data"> -->
                    <el-row :gutter="24">
                      <el-col :span="(this.fulldata1==0&&this.fulldata2==0)? 24:6" class="paddCol" v-if="this.control.length !== 0 || this.set.length !== 0">
                        <el-card class="box-card" style="height:797px;">
                          <div slot="header" class="clearfix">
                              <span><i class="el-icon-menu"></i> 模块状态</span>
                              <!-- <span><i class="el-icon-menu"></i> 空调控制</span> -->
                          </div>
                          <div :class="(this.fulldata1==0&&this.fulldata2==0)?'switch2':'switch'" style="display:inline-block;">
                            <div v-show="item.ready == 1" :key="item.index" v-for="item in control" class="switchLi">
                              <div class="item" style="display:flex">
                                <div class="normal_open overflowHidden">
                                  {{item.name}}
                                </div>
                                <div class="normal_open2" style="display:inline-block;">
                                  <el-switch @change="changeSt(item.id , item.value)"  v-model="item.value"  active-color="#13ce66" inactive-color="#ff4949"></el-switch>
                                </div>
                              </div>
                            </div>
                          </div>
                          <!-- 设定值循环 -->
                        <div class="set" style="margin-top:50px;display:inline-block;">
                          <div v-show="item.ready == 1"  :key="item.index" v-for="item in set" class="setLi">
                            <div class="item">
                              <div class="sheding overflowHidden">
                                <span class="name">{{item.name}}</span>
                              </div>
                              <div class="sheding2"  style="display:inline-block;">
                                <el-input-number v-model="item.value"  @change="changeSt(item.id,item.value)"  :min="item.min"
                                  :max="item.max" label="描述文字" ></el-input-number>
                              </div>
                            </div>
                          </div>
                        </div>
                        <!-- 设定值循环结束 -->
                        <br>
                        </el-card>
                    </el-col>
                    <el-col :span="(this.fulldata3==0&&this.fulldata4==0)? 24:18" :class="this.full == 1 ? 'fullClass paddCol' : 'paddCol'" v-if="this.fulldata1 != 0 || this.fulldata2 != 0">
                      <el-card class="box-card">
                        <div slot="header" class="clearfix">
                            <span><i class="el-icon-menu"></i> 模块数据</span>
                            <!-- <span><i class="el-icon-menu"></i>运行状态</span> -->
                        </div>
                        <div style="height:699px;overflow-y:scroll;">
            <!-- 右侧数据 -->
                        <div class="equipment_item" v-show="item.ready == 1" :key="item.index" v-for="item in humiExpand">
                            <div class="equipment_list">
                                <div class="equipment_list_name overflowHidden">{{item.name}}：</div>
                                <div class="equipment_list_value_div overflowHidden"><span class="equipment_list_value">{{parseFloat(item.value).toFixed(1)}}{{item.flag}}</span></div>
                            </div>
                        </div>
                        <div class="equipment_item" v-show="item.ready == 1" :key="item.index" v-for="item in humiExpand2">
                          <div class="equipment_list">
                            <div class="equipment_list_name overflowHidden">{{item.name}}：</div>
                            <div class="equipment_list_value_div overflowHidden"><span class="equipment_list_value">{{item.value == 0 ? item.alarmlowname : item.alarmhighname}}</span></div>
                          </div>
                        </div>
                        </div>
                        
                      </el-card>
                    </el-col>
                    </el-row>
              </div>
            </el-card>
        </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  props: ["msg"],
  data() {
    return {
      alarmstate:0,//在线离线
      timeGetData:'',//接口方法的定时器
      data: "", //当前设备数据
      dataShow: "", //当前设备数据  展示用数据
      clickGetData:'',//延迟保存计时器
      clickNumber:0,//延迟保存点击次
      dataor:1,//判断接口是否成功，1表示有返回成功，0表示失败，3之内无返回就置0表示失败
      control:'',
      set:'',
      humiExpand:[],
      humiExpand2:[],
      full:0,
      fulldata1:10,
      fulldata2:10,
      fulldata3:10,
      fulldata4:10,
      //属性数据
      firstconnecttimeYear:'',//首次上线年月日
      firstconnecttimeHour:'',//首次上线时分
      connecttimeYear:'',//最近上线年月日
      connecttimeHour:'',//最近上线时分
      totaltime:0,//累计运行时间
      localname:'',//本地储存名称
    };
  },
  mounted() {
  },
  created() {
    // 判断是否是直接访问当前设备页面
    if(localStorage.getItem('当前访问路径') == 'overview2'){
      this.localname = "6.0展示设备卡片"
    }else{
      this.localname = "当前设备编号"
    }
    this.info = JSON.parse(localStorage.getItem(this.localname))
    this.dataShow = JSON.parse(localStorage.getItem("menuDataJson")).unit
    this.data = JSON.parse(localStorage.getItem("menuDataJson"))
    this.getData(JSON.parse(localStorage.getItem(this.localname)))
    this.getDataStart()
  },
  methods: {
    //重新定时走接口
    getDataStart(){
      this.$nextTick(() => {
          this.timeGetData = setInterval(() => {
            if(this.dataor == 0){ //接口失败提示
              this.$message({
                message: '服务器访问失败',
                type: 'error',
                duration: 1000
              });
            }
            this.getData(JSON.parse(localStorage.getItem(this.localname)))
            this.dataor = 0
          }, 3000); 
      })
    },
    getData(info){
      this.info = info
      if(info.uid == null || info.uid == "null"){//清空缓存第一次进入时找不到info.uid，页面为空页面的处理
        info.uid = 0
      }
      var params = {
          "getdevinfo":[
            {
              "uid": info.uid,
              "type": info.type,
              "addr": info.addr,
              "unittype":"id-name-ready-value-flag-max-min-alarmhighname-alarmlowname"
            }
          ],
          [localStorage.getItem("cookieName")]:localStorage.getItem("_token")
      }
      this.$http.post(this.$baseUrl, params).then((res) => {
        this.dataor = 1// 接口成功置1
        if(res.data.getdevinfo == undefined){ //数据返回不全失败提示
          this.$message({
            message: '服务器访问失败',
            type: 'error',
            duration: 1000
          });
        }else{
          if(this.clickNumber == 0){ // 延迟判断状态中不接受真实状态
            var st = []
            for(var a = 0; a < res.data.getdevinfo[0].unit.length; a++){
              st.push(
                {
                  id:res.data.getdevinfo[0].unit[a].id, 
                  value:res.data.getdevinfo[0].unit[a].value, 
                  ready:res.data.getdevinfo[0].unit[a].ready, 
                  name:res.data.getdevinfo[0].unit[a].name, 
                  min:res.data.getdevinfo[0].unit[a].min, 
                  max:res.data.getdevinfo[0].unit[a].max
                }
              )
            }
            this.dataShow = st;//展示状态
          }
          this.data = res.data.getdevinfo[0];
          this.alarmstate = res.data.getdevinfo[0].alarmstate
          console.log(info.uid)
          this.getSwitchData(); //开关状态和设置值赋值
          this.rightData();// 拓展数据
          this.rightData2();// 拓展数据状态报警名
          // 上次上线时间
          this.firstconnecttimeYear = this.formatDate(this.data.firstconnecttime*1000, 'YYYY-MM-DD')
          this.firstconnecttimeHour = this.formatDate(this.data.firstconnecttime*1000, 'H:m')
          // 最近上线时间
          this.connecttimeYear = this.formatDate(this.data.connecttime*1000, 'YYYY-MM-DD')
          this.connecttimeHour = this.formatDate(this.data.connecttime*1000, 'H:m')
          //累计运行时间 拿到的分钟转成小时
          this.totaltime = (this.data.totaltime/60).toFixed(0)
        }
      })
    },
    //开关状态赋值
    getSwitchData() {
       //控制开关状态值
       var arr = [];
      for (let i = 23; i < 31; i++) {
        // this.dataShow[i].value = this.dataShow[i].value == 0 ? false : true;
        if (this.dataShow[i].ready == 1) {
          var str = {
            id:this.dataShow[i].id,
            ready:this.dataShow[i].ready,
            name:this.dataShow[i].name,
            value:this.dataShow[i].value == 0 ? false : true,
          }
          arr.push(str)
        }
      }
      this.control = arr;
      var num2 = 0
      for(let a = 0; a < this.control.length; a++){
        if(this.control[a].ready == 1){
          num2++;
        }
      }
      if(num2 == 0){
        this.fulldata4 = 0
      }else{
        this.fulldata4 = 10
      }
       //设置值状态值
      var set = [];
      for (let d = 31; d < 35; d++) {
        if (this.dataShow[d].ready == 1) {
          set.push(this.dataShow[d]);
        }
      }
      this.set = set;
      var num = 0
      for(let a = 0; a < this.set.length; a++){
        if(this.set[a].ready == 1){
          num++;
          
        }
      }
      if(num == 0){
        this.fulldata3 = 0
      }else{
        this.fulldata3 = 10
      }
    },
    // 拓展数据
    rightData() {
        //console.log(this.data.unit)
      var arr = [];
      if (this.data.unit.length !== 0) {
        for (let i = 0; i < 23; i++) {
          arr.push(this.data.unit[i]);
        }
      } else {
        var arr = new Array(5);
        var o = {
          ready: 1,
          name: null,
          value: 0,
          flag: ""
        };
        arr.fill(o);
      }
      this.humiExpand = arr
      var num = 0
      for(let a = 0; a < this.humiExpand.length; a++){
        if(this.humiExpand[a].ready == 1){
          num++;
        }
      }
      if(num == 0){
        this.fulldata1 = 0
      }else{
        this.fulldata1 = 10
      }
      // return arr;
    },
     // 拓展数据状态报警名
    rightData2() {
        //console.log(this.data.unit)
      var arr = [];
      if (this.data.unit.length !== 0) {
        for (let k = 35; k < 83; k++) {
          arr.push(this.data.unit[k]);
        }
      } else {
        var arr = new Array(5);
        var o = {
          ready: 1,
          name: null,
          value: 0,
          flag: ""
        };
        arr.fill(o);
      }
      this.humiExpand2 = arr
      var num = 0
      for(let a = 0; a < this.humiExpand2.length; a++){
        if(this.humiExpand2[a].ready == 1){
          num++;
        }
      }
      if(num == 0){
        this.fulldata2 = 0
      }else{
        this.fulldata2 = 10
      }
      // return arr;
    },
    // 修改开关状态
    changeSt(id, val) {
      console.log(id +','+val)
      var value = 0
      if(id >= 23 && id < 31){  //开关控制下发
        // this.control[index].value = val
        if(val == true){
          value = 1
        }else{
          value = 0
        }
      }else if(id >= 31 && id < 35){ //设置值下发
        value = Number(val)
      }else{
        return false
      }
      // 延迟保存方法
      clearInterval(this.clickGetData);
      this.clickGetData = null;
      this.clickNumber = 1;  //1秒后走逻辑，所以计时从1开始
      this.dataShow[id].value = value //修改展示状态值
      console.log('下发'+value)
      this.clickGetData = setInterval(() => { 
        this.clickNumber++;
        if(this.data.unit[id].value != this.dataShow[id].value){ // 返回的值和下发值不一致，等待返回
          console.log('错误等待'+this.clickNumber+'秒')
        }else{ // 返回的值和下发值一致，取消等待，拿取真实值
          console.log('正确')
          clearInterval(this.clickGetData);
          this.clickGetData = null;
          this.clickNumber = 0
        }
        if(this.clickNumber > this.$store.state.waittTime+1){ //等待5秒后，取消等待，拿取真实值 waittTime统一宏变量  计时从1开始，所以等待时间要再+1
          console.log('超时')
          clearInterval(this.clickGetData);
          this.clickGetData = null;
          this.clickNumber = 0
        }
      }, 1000); 
      var params = {
          "setdevinfo":[
              {
                  "uid": this.info.uid,
                  "type": this.info.type,
                  "addr": this.info.addr,
                  "unit":[
                    {
                      id: id,
                      value: value
                    }
                  ]
              }
          ],
          [localStorage.getItem("cookieName")]:localStorage.getItem("_token")
      }
      this.$http.post(this.$baseUrl, params).then((res) => {
          if(res.data.result == 'ok'){
              
          }else{
              this.$notify({
                  title: '操作失败',
                  type: 'warning',
                  duration: 1500
              });
          }
      });
    },
  },
  // 清除定时器
    beforeDestroy() {
        clearInterval(this.timeGetData);
        this.timeGetData = null;
        clearInterval(this.clickGetData);
        this.clickGetData = null;
    },
};
</script>

<style scoped>
.el-card {
  background: none;
  color: #fff;
  border: 1px solid rgba(89, 129, 165, 0.4);
}
/deep/ .el-card__header {
  background: rgba(89, 129, 165, 0.2);
  padding: 16px 20px;
  border-bottom: none;
}
.box-card {
  /* overflow: hidden; */
}
.temp_card{
  height: 850px;
}
.clearfix{
  font-size: 18px;
}
.shebeiname{
  font-weight: bold;
}
.xtg402_data{
  display: flex;
}
.mouse_data{
  width: 970px;
  height: 524px;
  margin-top: 98px;
  /* padding-right: 30px; */
  background: #0000002b;
}
.setLi{
  height: 60px;
  float: left;
}
.equipment_list_name{
    padding-left: 10px;
    width: 150px;
}
.equipment_list_value{
    color: #00fcff;
    font-size: 17px;
    font-weight: bold;
    padding: 0 10px;
}
.equipment_item{
      width: 25%;
    height: 70px;
    line-height: 70px;
    margin-bottom: 30px;
    float: left;
}
.equipment_list{
  width: 90%;
    height: 100%;
    margin: 0 auto;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    display: -webkit-flex;
    background: rgba(89, 129, 165, 0.2);
    overflow: hidden;
}
.equipment_list_value_div{
  width: 120px;
}
.switch{
  width: 100%;
  display: flex;
}
.switch2{
  width: 100%;
  display: flex;
}
.switch .switchLi{
  width: 100%;
  float: left;
  height: 50px;
  line-height: 50px;
}
.switch2 .switchLi{
  width: 30%;
  float: left;
  height: 100px;
  line-height: 100px;
}
.sheding{
  display:inline-block;
  width:100px;
  text-align: center;
  position: relative;
  top: 6px;
}
.normal_open{
  display:inline-block;
  width:155px;
  padding-right: 20px;
  position: relative;
  top: 4px;
  text-align: right;
}
.normal_open2{
  margin-left: 40px;
}
.sheding2{
  margin-left: 20px;
  margin-right: 20px;
}
.fullClass{
  width: 100%;
}
.fullClass .equipment_item{
  width: 20%;
}
/deep/ .el-input__inner{
  background-color: #ffffff05 !important;
  color: #fff;
  border: 1px solid rgba(89, 129, 165, 0.4);
}
/deep/ .el-input-number__increase{
  background: #fff0 !important;
  color: #fff;
  border-left: 1px solid rgba(89, 129, 165, 0.4);
}
/deep/ .el-input-number__decrease{
  background: #fff0 !important;
  color: #fff;
  border-right: 1px solid rgba(89, 129, 165, 0.4);
}
/* 在线时间 */
.temp_time_mode{
  width: 400px;
  height: 540px;
  /* margin: 0 auto; */
  padding-top: 30px;
  border-radius: 10px;
  background: #0000002b;
  margin-bottom: 20px;
  margin-top: 90px;
  margin-left: 50px;
}
.temp_peak_name{
  width: 100%;
  font-size: 22px;
  margin-left: 20px;
}
.temp_time{
  margin-top: 80px;
  margin-left: 25px;
}
.temp_time_line{
  margin-bottom: 90px;
  position: relative;
  top: 11px;
  display: flex;
  font-size: 20px;
}
.temp_time{
  margin-right: 30px;
  color: #d4d3d3;
}
.temp_time .temp_warning_title{
  width: 110px;
}
.temp_time_year{
  width: 150px;
  font-family: electronicFont;
  font-size: 28px;
  color: #5eceff;
}
.temp_time_hour{
  width: 40px;
  font-family: electronicFont;
  font-size: 28px;
  color: #5eceff;
}
.temp_time_h{
  font-family: electronicFont;
  font-size: 35px;
  color: #5eceff;
  position: relative;
  top: -5px;
}
.temp_time_s{
  color: #5eceff;
  margin-left: 5px;
}
.el-input-number{
  width: 160px;
}
</style>